
function noti_2_message(data) {
    let state = data['state'];
    let pid = data['pile_id'];
    let oid = data['num'];
    var type;

    switch (state) {
        case 2:
            message = "Please go to the " + pid + " pile and waiting for a moment, you will be the next one to charge!";
            type = "info";
            break;
        case 3:
            message = "Please go to the waiting area and be patient, your line number is " + oid + "!";
            type = "info";
            break;
        case 4:
            message = "Congratulations! Your charge request has been completed, welcome to come again!";
            type = "success";
            break;
        case 5:
            message = "Please go to the " + pid + " pile, ready to begin your charging!";
            type = "info";
            break;
        case 6:
            message = "Time out, you haven't begun your charging for a long time, cause automatic cancellation of charging!";
            type = "warning";
            break;
        default:
            message = "";
            type = "";
            break;
    }

    return { type: type, message: message, state: state };
}


function getNoti(ack, first = 0) {

    let dict = { ack: ack };
    if (first == 1) {
        dict['first'] = first;
    }

    $.ajax({
        type: 'GET',
        url: "/lpoll",
        data: dict,
        dataType: 'json',
        success: function (data) {
            if (Object.keys(data).length > 0) {
                dict = noti_2_message(data);
                if (dict['state'] !== 5) {
                    Swal.fire({
                        icon: dict['type'],
                        text: dict['message'],
                        position: 'top',
                        allowOutsideClick: false,
                        timer: 5000
                    });
                }

                if (typeof getCurrentState === 'function') {
                    currentState = dict['state'];
                    getCurrentState();
                }
            }
            getNoti(1);
        },
        error: function () {
            setTimeout(
                getNoti, 5000, 0
            );
        }
    });
}

getNoti(0, 1);